(function(vc) {
    vc.extends({
        data: {
            updateProductPriceInfo: {
                products: [],
                _currentProductName: '',
                productCategorys: [],
                categoryId: '',
                selectProducts: [],
                quanGroup: false,
                ruleId: ''
            }
        },
        watch: { // 监视双向绑定的数据数组
            'updateProductPriceInfo.selectProducts': {
                handler() { // 数据数组有变化将触发此函数
                    $that._computeQunFunc();
                },
                deep: true // 深度监视
            }
        },
        _initMethod: function() {
            $that._listProductCategorys();
        },
        _initEvent: function() {
            vc.on('updateProductPrice', 'openUpdateProductPriceModel', function(_param) {
                $('#updateProductPriceModel').modal('show');
                $that.updateProductPriceInfo.ruleId = _param.ruleId;
                $that._refreshUpdateProductPriceInfo();
                $that._loadAllProductInfo(1, 15, '');
            });
            vc.on('updateProductPrice', 'paginationPlus', 'page_event', function(_currentPage) {
                $that._loadAllProductInfo(_currentPage, 15);
            });
        },
        methods: {
            _computeQunFunc: function() {
                let _hasInSelectProducts = false;
                $that.updateProductPriceInfo.quanGroup = true;
                $that.updateProductPriceInfo.products.forEach(productItem => {
                    _hasInSelectProducts = false;
                    $that.updateProductPriceInfo.selectProducts.forEach(item => {
                        if (productItem.productId == item.productId) {
                            _hasInSelectProducts = true;
                        }
                    });
                    if (!_hasInSelectProducts) {
                        $that.updateProductPriceInfo.quanGroup = false;
                    }
                });
            },
            _loadAllProductInfo: function(_page, _row, _name) {
                let param = {
                    params: {
                        page: _page,
                        row: _row,
                        name: _name,
                        categoryId: $that.updateProductPriceInfo.categoryId
                    }
                };
                //发送get请求
                vc.http.apiGet('/product.queryProduct',
                    param,
                    function(json) {
                        var _productInfo = JSON.parse(json);
                        $that.updateProductPriceInfo.products = _productInfo.data;
                        vc.emit('updateProductPrice', 'paginationPlus', 'init', {
                            total: _productInfo.records,
                            currentPage: _page
                        });
                        $that._computeQunFunc();
                    },
                    function() {
                        console.log('请求失败处理');
                    }
                );
            },
            _listProductCategorys: function(_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/productCategory.queryProductCategory',
                    param,
                    function(json, res) {
                        let _productCategoryManageInfo = JSON.parse(json);
                        $that.updateProductPriceInfo.productCategorys = _productCategoryManageInfo.data;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _updateProductPrice: function() {
                let _data = {
                    shopId: vc.getCurrentCommunity().shopId,
                    ruleId: $that.updateProductPriceInfo.ruleId,
                    productIds: $that.updateProductPriceInfo.selectProducts.join(',')
                }
                vc.http.apiPost(
                    '/product.updateProductPrice',
                    JSON.stringify(_data), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#updateProductPriceModel').modal('hide');
                            $that._refreshUpdateProductPriceInfo();
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });

            },
            _closeUpdateProductPrice: function() {
                $('#updateProductPriceModel').modal('hide');
            },
            queryProducts: function() {
                $that._loadAllProductInfo(1, 15, $that.updateProductPriceInfo._currentProductName);
            },
            _refreshUpdateProductPriceInfo: function() {
                $that.updateProductPriceInfo._currentProductName = "";
                $that.updateProductPriceInfo.products = [];
                $that.updateProductPriceInfo.categoryId = '';
                $that.updateProductPriceInfo.selectProducts = [];
                $that.updateProductPriceInfo.quanGroup = false;
            },
            checkAllGroup: function(e) {
                var checkObj = document.querySelectorAll('.checkGroupItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            $that.updateProductPriceInfo.selectProducts.push(checkObj[i].value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.updateProductPriceInfo.products.forEach(productItem => {
                        let index = $that.updateProductPriceInfo.selectProducts.indexOf(productItem.productId);
                        if (index > -1) {
                            $that.updateProductPriceInfo.selectProducts.splice(index, 1);
                        }
                    });
                }
            }
        }
    });
})(window.vc);